package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class ShangTongSWTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;
	@Autowired
	OrderDao orderDao;
	@Autowired
	RabbitMqProducer rabbitMqProducer;//消息队列生产者
	
	Logger log = Logger.getLogger(getClass());
	
//	@Scheduled(fixedDelay = 60000)
	public void queryShangTongOrder(){ 
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = 2;//充值中
		try{
			String providerId = "0000000078";//尚通ID
			Map<String,Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);//获取通道
			String defaultParameter = (String) channel.get("default_parameter");//参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String action = "Query";//操作（固定值Charge）
			String account = paramMap.get("account");//平台申请的独立API账号
			String apiKey = paramMap.get("apiKey");//平台的appKey
			String queryUrl = paramMap.get("QueryUrl");//查询地址
			//查询订单中属于尚通的折扣
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", providerId);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);//查询出所有尚通的充值中的订单
			for(int i = 0 , size = orderList.size() ; i < size ; i ++){
				Map<String,Object> order = orderList.get(i);
				String orderId = order.get("orderId")+"";//平台订单号
				map.put("orderId", orderId);
				String timeStamp = System.currentTimeMillis()/1000+"";
				String signStr = apiKey + "account=" + account + "&action="+action+"&orderID=" + orderId + "&timeStamp=" + timeStamp + apiKey;
				String paramStr = "account="+account+"&action="+action+"&orderID="+orderId+"&sign="+MD5.ToMD5(signStr)+"&timeStamp="+timeStamp;
				String result = ToolHttp.post(false, queryUrl, paramStr, "application/x-www-form-urlencoded");
				if(result!=null && !result.equals("")){
					JSONObject resultJson = JSONObject.parseObject(result);//将返回值转为json对象
					String respCode = resultJson.getString("respCode");//返回状态码
					String respMsg = resultJson.getString("respMsg");//返回信息
					map.put("resultCode", respCode+":"+respMsg);
					if(respCode.equals("0002")){//充值成功
						flag = 1;
					}else if(respCode.equals("0000") || respCode.equals("0001")){//充值中或者下单成功
						continue;
					}else{//充值失败
						flag = 0;
					}
				}else{
					flag = -1;
				}
				map.put("status", flag);
				rabbitMqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		}catch(Exception e){
			flag = -1;
			log.error("尚通流量查询出错"+e);
		}
	}
	
}
